package com.hailou.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 彩票业务类
 * 
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class TicketService {
 
	
	public String getTicketMessage() {
		
		List<String> listTicket = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		/*******这里用数据其实更方便，但是如果请求地址变化的化不好改，所以我们采用map做包装**/
		Map<String,String> urlStringMap = new LinkedHashMap<String,String>();
		urlStringMap.put("ssq", "http://www.caipiaokong.com/api/lottery/xml.php?type=ssq");
		urlStringMap.put("dlt", "http://www.caipiaokong.com/api/lottery/xml.php?type=dlt");
		urlStringMap.put("qlc", "http://www.caipiaokong.com/api/lottery/xml.php?type=qlc");
		urlStringMap.put("qxc", "http://www.caipiaokong.com/api/lottery/xml.php?type=qxc");
		urlStringMap.put("sd", "http://www.caipiaokong.com/api/lottery/xml.php?type=sd");
		urlStringMap.put("pls", "http://www.caipiaokong.com/api/lottery/xml.php?type=pls");
		urlStringMap.put("plw", "http://www.caipiaokong.com/api/lottery/xml.php?type=plw");
		urlStringMap.put("eexw", "http://www.caipiaokong.com/api/lottery/xml.php?type=eexw");
		
		Set<Entry<String,String>> set = urlStringMap.entrySet();
		for(Iterator<Entry<String,String>> iterator = set.iterator();iterator.hasNext();){
			Entry<String,String> entry = iterator.next();
			try {
				URL url = new URL(entry.getValue());
				HttpURLConnection huc = (HttpURLConnection) url.openConnection();
				huc.setRequestMethod("GET");
				huc.setDoInput(true);
				sb.append(extractBallMessage(entry.getKey(),huc.getInputStream())).append("\n\n");
				url = null;
				huc.disconnect();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(sb.toString());
		return sb.toString();
	}

	@SuppressWarnings("unchecked")
	public String extractBallMessage(String key,InputStream in) {
		String type = "";
		if("ssq".equals(key)){
			type = "===双色球===";
		}else if("dlt".equals(key)){
			type = "===大乐透===";
		}else if("qlc".equals(key)){
			type = "===七乐彩===";
		}else if("qxc".equals(key)){
			type = "===七星彩===";
		}else if("sd".equals(key)){
			type = "===3D===";
		}else if("pls".equals(key)){
			type = "===排列三===";
		}else if("plw".equals(key)){
			type = "===排列五===";
		}else if("eexw".equals(key)){
			type = "===22选5===";
		}
		String result = "";
		SAXReader sax = new SAXReader();
		List<Element> list = null;
		try {
			Document doc = sax.read(in);
			Element root = doc.getRootElement();
			list = root.elements();
			Element element = list.get(0);
			result= type+"\n第"+element.attributeValue("expect")+"期\n开奖结果：\n"+element.attributeValue("opencode")+"\n开奖时间：\n"+element.attributeValue("opentime");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TicketService().getTicketMessage();
	}

}
